package com.portal.logic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {
	//private static String url = "jdbc:mysql://localhost/EmployeePortalDb";
	private static String url = null;
	//private static String db = "EmployeePortalDb";
	private static String driverName = "com.mysql.jdbc.Driver";   
	//private static String usernsme = "root";   
	//private static String pasword = "123";
	private static Connection con;

	public static Connection getConnection(String ip, String db, String usernsme, String pasword) throws Exception{
		url = "jdbc:mysql://"+ ip +"/" + db;
		//try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url, usernsme, pasword);
		/*} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
			System.out.println("Driver not found."); 
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("Failed to create the database connection."); 
		}*/
		return con;
	}

	public static void closeConnection(Connection connection){
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException ex) { 
			ex.printStackTrace();
		}
	}

	public static Connection getConnection(){
		if (con != null) {
			return con;
		}

		return null;
	}
}
